package com.sz.biz.logistics.bill.entity;

import com.sz.biz.common.approval.Approval;
import com.sz.biz.common.approval.IApprovalStatus;
import com.sz.common.base.dao.ext.IEnum;
import com.sz.common.base.i18n.I18nMessages;

/**
 * Function: 账单状态 <br>
 * Author: jifeixing<br>
 * Date: 2017-05-18 17:03:00
 */
public enum BillingStatus implements IEnum, IApprovalStatus {

    GENERATION("0", "生成账单"),
    DRAFT("1", "草稿"),
    AUDITING("2", "待审核"),
    ADJUSTPRICE("21", "修改价格"),
    CHARGEOFF("3", "已出账"),
    PAID("4", "已付款"),
    OVERDUE("5", "逾期未结");


    private String code;
    private String name;

    BillingStatus(String code, String name) {
        this.code = code;
        this.name = name;
    }

    @Override
    public String getCode() {
        return this.code;
    }

    @Override
    public String getName() {
        return this.name;
    }
    @Override
    public int getValue() {
        return Integer.valueOf(this.getCode());
    }

    public static BillingStatus valueOfEnum(String code) {
        BillingStatus[] iss = values();
        for (BillingStatus cs : iss) {
            if (cs.getCode().equals(code)) {
                return cs;
            }
        }
        return null;
    }

    @Override
    public boolean isTransitive(IApprovalStatus status) {
        if (this.equals(GENERATION) && DRAFT.equals(status)) {
            return true;
        }
        if (this.equals(DRAFT) && AUDITING.equals(status)) {
            return true;
        }
        if (this.equals(AUDITING) && (DRAFT.equals(status) || CHARGEOFF.equals(status) || ADJUSTPRICE.equals(status))) {
            return true;
        }
        if (this.equals(CHARGEOFF) && PAID.equals(status)) {
            return true;
        }
        return this.equals(PAID) && OVERDUE.equals(status);
    }

    /**
     * 获取审核动作名称
     * @param i18nMessages
     * @param approval
     * @return
     */
    @Override
    public String getActionName(I18nMessages i18nMessages, Approval approval) {
        //TODO: 支持国际化，不要硬编码字符 --lqb
        String action = "";
        if (approval != null) {
            if (approval.getFromStatus().toString().equals(BillingStatus.GENERATION.getCode())
                    && approval.getToStatus().toString().equals(BillingStatus.DRAFT.getCode())) {
                action = "生成账单";
            }
            if (approval.getFromStatus().toString().equals(BillingStatus.DRAFT.getCode())
                    && approval.getToStatus().toString().equals(BillingStatus.AUDITING.getCode())) {
                action = "提交审核";
            }
            if (approval.getFromStatus().toString().equals(BillingStatus.AUDITING.getCode())) {
                if (approval.getToStatus().toString().equals(BillingStatus.DRAFT.getCode())) {
                    action = "打回草稿";
                }
                if (approval.getToStatus().toString().equals(BillingStatus.CHARGEOFF.getCode())) {
                    action = "审核通过";
                }
                if (approval.getToStatus().toString().equals(BillingStatus.ADJUSTPRICE.getCode())) {
                    action = "修改价格";
                }
            }
            if (approval.getFromStatus().toString().equals(BillingStatus.CHARGEOFF.getCode())
                    && approval.getToStatus().toString().equals(BillingStatus.PAID.getCode())) {
                action = "客户支付";
            }
        }
        return action;
    }
}
